home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / jpeg / lib / jrdjfif.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-27  |  23.5 KB  |  863 lines

  1. /*
  2.  * jrdjfif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to decode standard JPEG file headers/markers.
  9.  * This code will handle "raw JPEG" and JFIF-convention JPEG files.
  10.  *
  11.  * You can also use this module to decode a raw-JPEG or JFIF-standard data
  12.  * stream that is embedded within a larger file.  To do that, you must
  13.  * position the file to the JPEG SOI marker (0xFF/0xD8) that begins the
  14.  * data sequence to be decoded.  If nothing better is possible, you can scan
  15.  * the file until you see the SOI marker, then use JUNGETC to push it back.
  16.  *
  17.  * This module relies on the JGETC macro and the read_jpeg_data method (which
  18.  * is provided by the user interface) to read from the JPEG data stream.
  19.  * Therefore, this module is not dependent on any particular assumption about
  20.  * the data source; it need not be a stdio stream at all.  (This fact does
  21.  * NOT carry over to more complex JPEG file formats such as JPEG-in-TIFF;
  22.  * those format control modules may well need to assume stdio input.)
  23.  *
  24.  * These routines are invoked via the methods read_file_header,
  25.  * read_scan_header, read_jpeg_data, read_scan_trailer, and read_file_trailer.
  26. %
  27. % Modified:     Jin Guojun - LBL, Image Technology Group
  28. %       Date:   April 14, 1992
  29. %       Goal:   To be easily handled by conversion library - CCS (c)
  30. %        HIPS, FITS, GIF, RLE, SUN-raster, PNM, TIFF, PICT ...
  31. %        can be compressed by cjpeg now, directly displayed by tuner,
  32. %        decompressed to other image type by torle, torast, and color_ps.
  33. %        These type of images can be determined by program `headers'.
  34.  */
  35.  
  36. #include "jinclude.h"
  37.  
  38. #ifdef JFIF_SUPPORTED
  39.  
  40.  
  41. typedef enum {            /* JPEG marker codes */
  42.   M_SOF0  = 0xc0,
  43.   M_SOF1  = 0xc1,
  44.   M_SOF2  = 0xc2,
  45.   M_SOF3  = 0xc3,
  46.   
  47.   M_SOF5  = 0xc5,
  48.   M_SOF6  = 0xc6,
  49.   M_SOF7  = 0xc7,
  50.   
  51.   M_JPG   = 0xc8,
  52.   M_SOF9  = 0xc9,
  53.   M_SOF10 = 0xca,
  54.   M_SOF11 = 0xcb,
  55.   
  56.   M_SOF13 = 0xcd,
  57.   M_SOF14 = 0xce,
  58.   M_SOF15 = 0xcf,
  59.   
  60.   M_DHT   = 0xc4,
  61.   
  62.   M_DAC   = 0xcc,
  63.   
  64.   M_RST0  = 0xd0,
  65.   M_RST1  = 0xd1,
  66.   M_RST2  = 0xd2,
  67.   M_RST3  = 0xd3,
  68.   M_RST4  = 0xd4,
  69.   M_RST5  = 0xd5,
  70.   M_RST6  = 0xd6,
  71.   M_RST7  = 0xd7,
  72.   
  73.   M_SOI   = 0xd8,
  74.   M_EOI   = 0xd9,
  75.   M_SOS   = 0xda,
  76.   M_DQT   = 0xdb,
  77.   M_DNL   = 0xdc,
  78.   M_DRI   = 0xdd,
  79.   M_DHP   = 0xde,
  80.   M_EXP   = 0xdf,
  81.   
  82.   M_APP0  = 0xe0,
  83.   M_APP15 = 0xef,
  84.   
  85.   M_JPG0  = 0xf0,
  86.   M_JPG13 = 0xfd,
  87.   M_COM   = 0xfe,
  88.   
  89.   M_TEM   = 0x01,
  90.   
  91.   M_ERROR = 0x100
  92. } JPEG_MARKER;
  93.  
  94.  
  95. /*
  96.  * Reload the input buffer after it's been emptied, and return the next byte.
  97.  * This is exported for direct use by the entropy decoder.
  98.  * See the JGETC macro for calling conditions.  Note in particular that
  99.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  100.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  101.  * For error recovery purposes, synthesizing an EOI marker is probably best.
  102.  *
  103.  * For this header control module, read_jpeg_data is supplied by the
  104.  * user interface.  However, header formats that require random access
  105.  * to the input file would need to supply their own code.  This code is
  106.  * left here to indicate what is required.
  107.  */
  108.  
  109. #if 0                /* not needed in this module */
  110.  
  111. METHODDEF int
  112. read_jpeg_data (decompress_info_ptr cinfo)
  113. {
  114.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  115.  
  116.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  117.                     cinfo->next_input_byte,
  118.                     JPEG_BUF_SIZE);
  119.   
  120.   if (cinfo->bytes_in_buffer <= 0) {
  121.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  122.     cinfo->next_input_byte[0] = (char) 0xFF;
  123.     cinfo->next_input_byte[1] = (char) M_EOI;
  124.     cinfo->bytes_in_buffer = 2;
  125.   }
  126.  
  127.   return JGETC(cinfo);
  128. }
  129.  
  130. #endif
  131.  
  132.  
  133. /*
  134.  * Routines to parse JPEG markers & save away the useful info.
  135.  */
  136.  
  137.  
  138. LOCAL INT32
  139. get_2bytes (decompress_info_ptr cinfo)
  140. /* Get a 2-byte unsigned integer (e.g., a marker parameter length field) */
  141. {
  142.   INT32 a;
  143.   
  144.   a = JGETC(cinfo);
  145.   return (a << 8) + JGETC(cinfo);
  146. }
  147.  
  148.  
  149. LOCAL void
  150. skip_variable (decompress_info_ptr cinfo, int code)
  151. /* Skip over an unknown or uninteresting variable-length marker */
  152. {
  153.   INT32 length;
  154.   
  155.   length = get_2bytes(cinfo);
  156.   
  157.   TRACEMS2(cinfo->emethods, 1,
  158.        "Skipping marker 0x%02x, length %u", code, (int) length);
  159.   
  160.   for (length -= 2; length > 0; length--)
  161.     (void) JGETC(cinfo);
  162. }
  163.  
  164.  
  165. LOCAL void
  166. get_dht (decompress_info_ptr cinfo)
  167. /* Process a DHT marker */
  168. {
  169.   INT32 length;
  170.   UINT8 bits[17];
  171.   UINT8 huffval[256];
  172.   int i, index, count;
  173.   HUFF_TBL **htblptr;
  174.   
  175.   length = get_2bytes(cinfo)-2;
  176.   
  177.   while (length > 0) {
  178.     index = JGETC(cinfo);
  179.  
  180.     TRACEMS1(cinfo->emethods, 1, "Define Huffman Table 0x%02x", index);
  181.       
  182.     bits[0] = 0;
  183.     count = 0;
  184.     for (i = 1; i <= 16; i++) {
  185.       bits[i] = (UINT8) JGETC(cinfo);
  186.       count += bits[i];
  187.     }
  188.  
  189.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  190.          bits[1], bits[2], bits[3], bits[4],
  191.          bits[5], bits[6], bits[7], bits[8]);
  192.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  193.          bits[9], bits[10], bits[11], bits[12],
  194.          bits[13], bits[14], bits[15], bits[16]);
  195.  
  196.     if (count > 256)
  197.       ERREXIT(cinfo->emethods, "Bogus DHT counts");
  198.  
  199.     for (i = 0; i < count; i++)
  200.       huffval[i] = (UINT8) JGETC(cinfo);
  201.  
  202.     length -= 1 + 16 + count;
  203.  
  204.     if (index & 0x10) {        /* AC table definition */
  205.       index -= 0x10;
  206.       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  207.     } else {            /* DC table definition */
  208.       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  209.     }
  210.  
  211.     if (index < 0 || index >= NUM_HUFF_TBLS)
  212.       ERREXIT1(cinfo->emethods, "Bogus DHT index %d", index);
  213.  
  214.     if (*htblptr == NULL)
  215.       *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  216.   
  217.     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  218.     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  219.     }
  220. }
  221.  
  222.  
  223. LOCAL void
  224. get_dac (decompress_info_ptr cinfo)
  225. /* Process a DAC marker */
  226. {
  227.   INT32 length;
  228.   int index, val;
  229.  
  230.   length = get_2bytes(cinfo)-2;
  231.   
  232.   while (length > 0) {
  233.     index = JGETC(cinfo);
  234.     val = JGETC(cinfo);
  235.  
  236.     TRACEMS2(cinfo->emethods, 1,
  237.          "Define Arithmetic Table 0x%02x: 0x%02x", index, val);
  238.  
  239.     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  240.       ERREXIT1(cinfo->emethods, "Bogus DAC index %d", index);
  241.  
  242.     if (index >= NUM_ARITH_TBLS) { /* define AC table */
  243.       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  244.     } else {            /* define DC table */
  245.       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  246.       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  247.       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  248.     ERREXIT1(cinfo->emethods, "Bogus DAC value 0x%x", val);
  249.     }
  250.  
  251.     length -= 2;
  252.   }
  253. }
  254.  
  255.  
  256. LOCAL void
  257. get_dqt (decompress_info_ptr cinfo)
  258. /* Process a DQT marker */
  259. {
  260.   INT32 length;
  261.   int n, i, prec;
  262.   UINT16 tmp;
  263.   QUANT_TBL_PTR quant_ptr;
  264.   
  265.   length = get_2bytes(cinfo) - 2;
  266.   
  267.   while (length > 0) {
  268.     n = JGETC(cinfo);
  269.     prec = n >> 4;
  270.     n &= 0x0F;
  271.  
  272.     TRACEMS2(cinfo->emethods, 1,
  273.          "Define Quantization Table %d  precision %d", n, prec);
  274.  
  275.     if (n >= NUM_QUANT_TBLS)
  276.       ERREXIT1(cinfo->emethods, "Bogus table number %d", n);
  277.       
  278.     if (cinfo->quant_tbl_ptrs[n] == NULL)
  279.       cinfo->quant_tbl_ptrs[n] = (QUANT_TBL_PTR)
  280.     (*cinfo->emethods->alloc_small) (SIZEOF(QUANT_TBL));
  281.     quant_ptr = cinfo->quant_tbl_ptrs[n];
  282.  
  283.     for (i = 0; i < DCTSIZE2; i++) {
  284.       tmp = JGETC(cinfo);
  285.       if (prec)
  286.     tmp = (tmp<<8) + JGETC(cinfo);
  287.       quant_ptr[i] = tmp;
  288.     }
  289.  
  290.     for (i = 0; i < DCTSIZE2; i += 8) {
  291.       TRACEMS8(cinfo->emethods, 2, "        %4u %4u %4u %4u %4u %4u %4u %4u",
  292.            quant_ptr[i  ], quant_ptr[i+1], quant_ptr[i+2], quant_ptr[i+3],
  293.            quant_ptr[i+4], quant_ptr[i+5], quant_ptr[i+6], quant_ptr[i+7]);
  294.     }
  295.  
  296.     length -= DCTSIZE2+1;
  297.     if (prec) length -= DCTSIZE2;
  298.   }
  299. }
  300.  
  301.  
  302. LOCAL void
  303. get_dri (decompress_info_ptr cinfo)
  304. /* Process a DRI marker */
  305. {
  306.   if (get_2bytes(cinfo) != 4)
  307.     ERREXIT(cinfo->emethods, "Bogus length in DRI");
  308.  
  309.   cinfo->restart_interval = (UINT16) get_2bytes(cinfo);
  310.  
  311.   TRACEMS1(cinfo->emethods, 1,
  312.        "Define Restart Interval %u", cinfo->restart_interval);
  313. }
  314.  
  315.  
  316. LOCAL void
  317. get_app0 (decompress_info_ptr cinfo)
  318. /* Process an APP0 marker */
  319. {
  320. #define JFIF_LEN 14
  321.   INT32 length;
  322.   UINT8 b[JFIF_LEN];
  323.   int buffp;
  324.  
  325.   length = get_2bytes(cinfo) - 2;
  326.  
  327.   /* See if a JFIF APP0 marker is present */
  328.  
  329.   if (length >= JFIF_LEN) {
  330.     for (buffp = 0; buffp < JFIF_LEN; buffp++)
  331.       b[buffp] = (UINT8) JGETC(cinfo);
  332.     length -= JFIF_LEN;
  333.  
  334.     if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
  335.       /* Found JFIF APP0 marker: check version */
  336.       /* Major version must be 1 */
  337.       if (b[5] != 1)
  338.     ERREXIT2(cinfo->emethods, "Unsupported JFIF revision number %d.%02d",
  339.          b[5], b[6]);
  340.       /* Minor version should be 0..2, but try to process anyway if newer */
  341.       if (b[6] > 2)
  342.     TRACEMS2(cinfo->emethods, 1, "Warning: unknown JFIF revision number %d.%02d",
  343.          b[5], b[6]);
  344.       /* Save info */
  345.       cinfo->density_unit = b[7];
  346.       cinfo->X_density = (b[8] << 8) + b[9];
  347.       cinfo->Y_density = (b[10] << 8) + b[11];
  348.       /* Assume colorspace is YCbCr, unless UI has overridden me */
  349.       if (cinfo->jpeg_color_space == CS_UNKNOWN)
  350.     cinfo->jpeg_color_space = CS_YCbCr;
  351.       TRACEMS3(cinfo->emethods, 1, "JFIF APP0 marker, density %dx%d  %d",
  352.            cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  353.       if (b[12] | b[13])
  354.     TRACEMS2(cinfo->emethods, 1, "    with %d x %d thumbnail image",
  355.          b[12], b[13]);
  356.       if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  357.     TRACEMS1(cinfo->emethods, 1,
  358.          "Warning: thumbnail image size does not match data length %u",
  359.          (int) length);
  360.     } else {
  361.       TRACEMS1(cinfo->emethods, 1, "Unknown APP0 marker (not JFIF), length %u",
  362.            (int) length + JFIF_LEN);
  363.     }
  364.   } else {
  365.     TRACEMS1(cinfo->emethods, 1, "Short APP0 marker, length %u", (int) length);
  366.   }
  367.  
  368.   while (length-- > 0)    {    /* skip any remaining data */
  369.     if (JGETC(cinfo) == 0xFF && length > 2) {
  370.         length--;
  371.         if (JGETC(cinfo) == 0x7F)   {
  372.             cinfo->img->frames = get_2bytes(cinfo);
  373.             length -= 2;
  374.         }
  375.     }
  376.   }
  377. }
  378.  
  379.  
  380. LOCAL void
  381. get_sof (decompress_info_ptr cinfo, int code)
  382. /* Process a SOFn marker */
  383. {
  384.   INT32 length;
  385.   short ci;
  386.   int c;
  387.   jpeg_component_info * compptr;
  388.   
  389.   length = get_2bytes(cinfo);
  390.   
  391.   cinfo->data_precision = JGETC(cinfo);
  392.   cinfo->image_height   = get_2bytes(cinfo);
  393.   cinfo->image_width    = get_2bytes(cinfo);
  394.   cinfo->num_components = JGETC(cinfo);
  395.  
  396.   TRACEMS4(cinfo->emethods, 1,
  397.        "Start Of Frame 0x%02x: width=%u, height=%u, components=%d",
  398.        code, (int) cinfo->image_width, (int) cinfo->image_height,
  399.        cinfo->num_components);
  400.  
  401.   /* We don't support files in which the image height is initially specified */
  402.   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  403.   /* might as well have a general sanity check. */
  404.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  405.       || cinfo->num_components <= 0)
  406.     ERREXIT(cinfo->emethods, "Empty JPEG image (DNL not supported)");
  407.  
  408. #ifdef EIGHT_BIT_SAMPLES
  409.   if (cinfo->data_precision != 8)
  410.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  411. #endif
  412. #ifdef TWELVE_BIT_SAMPLES
  413.   if (cinfo->data_precision != 12) /* this needs more thought?? */
  414.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  415. #endif
  416. #ifdef SIXTEEN_BIT_SAMPLES
  417.   if (cinfo->data_precision != 16) /* this needs more thought?? */
  418.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  419. #endif
  420.  
  421.   if (length != (cinfo->num_components * 3 + 8))
  422.     ERREXIT(cinfo->emethods, "Bogus SOF length");
  423.  
  424.   cinfo->comp_info = (jpeg_component_info *) (*cinfo->emethods->alloc_small)
  425.             (cinfo->num_components * SIZEOF(jpeg_component_info));
  426.   
  427.   for (ci = 0; ci < cinfo->num_components; ci++) {
  428.     compptr = &cinfo->comp_info[ci];
  429.     compptr->component_index = ci;
  430.     compptr->component_id = JGETC(cinfo);
  431.     c = JGETC(cinfo);
  432.     compptr->h_samp_factor = (c >> 4) & 15;
  433.     compptr->v_samp_factor = (c     ) & 15;
  434.     compptr->quant_tbl_no  = JGETC(cinfo);
  435.       
  436.     TRACEMS4(cinfo->emethods, 1, "    Component %d: %dhx%dv q=%d",
  437.          compptr->component_id, compptr->h_samp_factor,
  438.          compptr->v_samp_factor, compptr->quant_tbl_no);
  439.   }
  440. }
  441.  
  442.  
  443. LOCAL void
  444. get_sos (decompress_info_ptr cinfo)
  445. /* Process a SOS marker */
  446. {
  447.   INT32 length;
  448.   int i, ci, n, c, cc;
  449.   jpeg_component_info * compptr;
  450.   
  451.   length = get_2bytes(cinfo);
  452.   
  453.   n = JGETC(cinfo);  /* Number of components */
  454.   cinfo->comps_in_scan = n;
  455.   length -= 3;
  456.   
  457.   if (length != (n * 2 + 3) || n < 1 || n > MAX_COMPS_IN_SCAN)
  458.     ERREXIT(cinfo->emethods, "Bogus SOS length");
  459.  
  460.   TRACEMS1(cinfo->emethods, 1, "Start Of Scan: %d components", n);
  461.   
  462.   for (i = 0; i < n; i++) {
  463.     cc = JGETC(cinfo);
  464.     c = JGETC(cinfo);
  465.     length -= 2;
  466.     
  467.     for (ci = 0; ci < cinfo->num_components; ci++)
  468.       if (cc == cinfo->comp_info[ci].component_id)
  469.     break;
  470.     
  471.     if (ci >= cinfo->num_components)
  472.       ERREXIT(cinfo->emethods, "Invalid component number in SOS");
  473.     
  474.     compptr = &cinfo->comp_info[ci];
  475.     cinfo->cur_comp_info[i] = compptr;
  476.     compptr->dc_tbl_no = (c >> 4) & 15;
  477.     compptr->ac_tbl_no = (c     ) & 15;
  478.     
  479.     TRACEMS3(cinfo->emethods, 1, "    c%d: [dc=%d ac=%d]", cc,
  480.          compptr->dc_tbl_no, compptr->ac_tbl_no);
  481.   }
  482.   
  483.   while (length > 0) {
  484.     (void) JGETC(cinfo);
  485.     length--;
  486.   }
  487. }
  488.  
  489.  
  490. LOCAL void
  491. get_soi (decompress_info_ptr cinfo)
  492. /* Process an SOI marker */
  493. {
  494.   int i;
  495.   
  496.   TRACEMS(cinfo->emethods, 1, "Start of Image");
  497.  
  498.   /* Reset all parameters that are defined to be reset by SOI */
  499.  
  500.   for (i = 0; i < NUM_ARITH_TBLS; i++) {
  501.     cinfo->arith_dc_L[i] = 0;
  502.     cinfo->arith_dc_U[i] = 1;
  503.     cinfo->arith_ac_K[i] = 5;
  504.   }
  505.   cinfo->restart_interval = 0;
  506.  
  507.   cinfo->density_unit = 0;    /* set default JFIF APP0 values */
  508.   cinfo->X_density = 1;
  509.   cinfo->Y_density = 1;
  510.  
  511.   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling */
  512. }
  513.  
  514.  
  515. LOCAL int
  516. next_marker (decompress_info_ptr cinfo)
  517. /* Find the next JPEG marker */
  518. /* Note that the output might not be a valid marker code, */
  519. /* but it will never be 0 or FF */
  520. {
  521.   int c, nbytes=0;
  522.  
  523.   do {
  524.     do {            /* skip any non-FF bytes */
  525.       nbytes++;
  526.       c = JGETC(cinfo);
  527. /*
  528.     if (nbytes > 1)    fprintf(stderr, "%X, %d(0x%x) %d(0x%x)    ",
  529.         c, ftell(cinfo->input_file), cinfo->input_file, ftell(cinfo->img->IN_FP), cinfo->img->IN_FP);
  530. */
  531.     } while (c != 0xFF && c != EOF);
  532.     do {            /* skip any duplicate FFs */
  533.       /* we don't increment nbytes here since extra FFs are legal */
  534.       c = JGETC(cinfo);
  535.     } while (c == 0xFF);
  536.   } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  537.  
  538.   if (nbytes != 1)
  539.     WARNMS2(cinfo->emethods,
  540.         "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
  541.         nbytes-1, c);
  542. #ifdef    _DEBUG_
  543.   if (c==0xda)    fprintf(stderr, "floc = %x    ", ftell(cinfo->input_file));
  544. #endif
  545.   return c;
  546. }
  547.  
  548.  
  549. LOCAL JPEG_MARKER
  550. process_tables (decompress_info_ptr cinfo)
  551. /* Scan and process JPEG markers that can appear in any order */
  552. /* Return when an SOI, EOI, SOFn, or SOS is found */
  553. {
  554.   int c;
  555.  
  556.   while (TRUE) {
  557.     c = next_marker(cinfo);
  558.       
  559.     switch (c) {
  560.     case M_SOF0:
  561.     case M_SOF1:
  562.     case M_SOF2:
  563.     case M_SOF3:
  564.     case M_SOF5:
  565.     case M_SOF6:
  566.     case M_SOF7:
  567.     case M_JPG:
  568.     case M_SOF9:
  569.     case M_SOF10:
  570.     case M_SOF11:
  571.     case M_SOF13:
  572.     case M_SOF14:
  573.     case M_SOF15:
  574.     case M_SOI:
  575.     case M_EOI:
  576.     case M_SOS:
  577.       return ((JPEG_MARKER) c);
  578.       
  579.     case M_DHT:
  580.       get_dht(cinfo);
  581.       break;
  582.       
  583.     case M_DAC:
  584.       get_dac(cinfo);
  585.       break;
  586.       
  587.     case M_DQT:
  588.       get_dqt(cinfo);
  589.       break;
  590.       
  591.     case M_DRI:
  592.       get_dri(cinfo);
  593.       break;
  594.       
  595.     case M_APP0:
  596.       get_app0(cinfo);
  597.       break;
  598.  
  599.     case M_RST0:        /* these are all parameterless */
  600.     case M_RST1:
  601.     case M_RST2:
  602.     case M_RST3:
  603.     case M_RST4:
  604.     case M_RST5:
  605.     case M_RST6:
  606.     case M_RST7:
  607.     case M_TEM:
  608.       TRACEMS1(cinfo->emethods, 1, "Unexpected marker 0x%02x", c);
  609.       break;
  610.  
  611.     default:    /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn */
  612.       skip_variable(cinfo, c);
  613.       break;
  614.     }
  615.   }
  616. }
  617.  
  618.  
  619.  
  620. /*
  621.  * Initialize and read the file header (everything through the SOF marker).
  622.  */
  623.  
  624. METHODDEF
  625. read_file_header (decompress_info_ptr cinfo)
  626. {
  627.   int c;
  628.  
  629.   /* Demand an SOI marker at the start of the file --- otherwise it's
  630.    * probably not a JPEG file at all.  If the user interface wants to support
  631.    * nonstandard headers in front of the SOI, it must skip over them itself
  632.    * before calling jpeg_decompress().
  633.    */
  634.   if (JGETC(cinfo) != 0xFF  ||  JGETC(cinfo) != M_SOI)
  635.     return    EOF;
  636.  
  637.   get_soi(cinfo);        /* OK, process SOI */
  638.  
  639.   /* Process markers until SOF */
  640.   c = process_tables(cinfo);
  641.  
  642.   switch (c) {
  643.   case M_SOF0:
  644.   case M_SOF1:
  645.     get_sof(cinfo, c);
  646.     cinfo->arith_code = FALSE;
  647.     break;
  648.       
  649.   case M_SOF9:
  650.     get_sof(cinfo, c);
  651.     cinfo->arith_code = TRUE;
  652.     break;
  653.  
  654.   default:
  655.     ERREXIT1(cinfo->emethods, "Unsupported SOF marker type 0x%02x", c);
  656.     break;
  657.   }
  658.  
  659.   /* Figure out what colorspace we have */
  660.   /* (too bad the JPEG committee didn't provide a real way to specify this) */
  661.  
  662.   switch (cinfo->num_components) {
  663.   case 1:
  664.     cinfo->jpeg_color_space = CS_GRAYSCALE;
  665.     break;
  666.  
  667.   case 3:
  668.     /* if we saw a JFIF marker, leave it set to YCbCr; */
  669.     /* also leave it alone if UI has provided a value */
  670.     if (cinfo->jpeg_color_space == CS_UNKNOWN) {
  671.       short cid0 = cinfo->comp_info[0].component_id;
  672.       short cid1 = cinfo->comp_info[1].component_id;
  673.       short cid2 = cinfo->comp_info[2].component_id;
  674.  
  675.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  676.     cinfo->jpeg_color_space = CS_YCbCr; /* assume it's JFIF w/out marker */
  677.       else if (cid0 == 1 && cid1 == 4 && cid2 == 5)
  678.     cinfo->jpeg_color_space = CS_YIQ; /* prototype's YIQ matrix */
  679.       else {
  680.     TRACEMS3(cinfo->emethods, 1,
  681.          "Unrecognized component IDs %d %d %d, assuming YCbCr",
  682.          cid0, cid1, cid2);
  683.     cinfo->jpeg_color_space = CS_YCbCr;
  684.       }
  685.     }
  686.     break;
  687.  
  688.   case 4:
  689.     cinfo->jpeg_color_space = CS_CMYK;
  690.     break;
  691.  
  692.   default:
  693.     cinfo->jpeg_color_space = CS_UNKNOWN;
  694.     break;
  695.   }
  696. }
  697.  
  698.  
  699. /*
  700.  * Read the start of a scan (everything through the SOS marker).
  701.  * Return TRUE if find SOS, FALSE if find EOI.
  702.  */
  703.  
  704. METHODDEF boolean
  705. read_scan_header (decompress_info_ptr cinfo)
  706. {
  707.   int c;
  708.   
  709.   /* Process markers until SOS or EOI */
  710.   c = process_tables(cinfo);
  711.   
  712.   switch (c) {
  713.   case M_SOS:
  714.     get_sos(cinfo);
  715.     return TRUE;
  716.     
  717.   case M_EOI:
  718.     TRACEMS(cinfo->emethods, 1, "End Of Image");
  719.     return FALSE;
  720.  
  721.   default:
  722.     return    prgmerr(0, "Unexpected marker 0x%02x", c);
  723.     break;
  724.   }
  725.   return FALSE;            /* keeps lint happy */
  726. }
  727.  
  728.  
  729. /*
  730.  * The entropy decoder calls this routine if it finds a marker other than
  731.  * the restart marker it was expecting.  (This code is *not* used unless
  732.  * a nonzero restart interval has been declared.)  The passed parameter is
  733.  * the marker code actually found (might be anything, except 0 or FF).
  734.  * The desired restart marker is that indicated by cinfo->next_restart_num.
  735.  * This routine is supposed to apply whatever error recovery strategy seems
  736.  * appropriate in order to position the input stream to the next data segment.
  737.  * For some file formats (eg, TIFF) extra information such as tile boundary
  738.  * pointers may be available to help in this decision.
  739.  *
  740.  * This implementation is substantially constrained by wanting to treat the
  741.  * input as a data stream; this means we can't back up.  (For instance, we
  742.  * generally can't fseek() if the input is a Unix pipe.)  Therefore, we have
  743.  * only the following actions to work with:
  744.  *   1. Do nothing, let the entropy decoder resume at next byte of file.
  745.  *   2. Read forward until we find another marker, discarding intervening
  746.  *      data.  (In theory we could look ahead within the current bufferload,
  747.  *      without having to discard data if we don't find the desired marker.
  748.  *      This idea is not implemented here, in part because it makes behavior
  749.  *      dependent on buffer size and chance buffer-boundary positions.)
  750.  *   3. Push back the passed marker (with JUNGETC).  This will cause the
  751.  *      entropy decoder to process an empty data segment, inserting dummy
  752.  *      zeroes, and then re-read the marker we pushed back.
  753.  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  754.  * appropriate if the found marker is a future restart marker (indicating
  755.  * that we have missed the desired restart marker, probably because it got
  756.  * corrupted).
  757.  
  758.  * We apply #2 or #3 if the found marker is a restart marker no more than
  759.  * two counts behind or ahead of the expected one.  We also apply #2 if the
  760.  * found marker is not a legal JPEG marker code (it's certainly bogus data).
  761.  * If the found marker is a restart marker more than 2 counts away, we do #1
  762.  * (too much risk that the marker is erroneous; with luck we will be able to
  763.  * resync at some future point).
  764.  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
  765.  * overrunning the end of a scan.  An implementation limited to single-scan
  766.  * files might find it better to apply #2 for markers other than EOI, since
  767.  * any other marker would have to be bogus data in that case.
  768.  */
  769.  
  770. METHODDEF void
  771. resync_to_restart (decompress_info_ptr cinfo, int marker)
  772. {
  773.   int desired = cinfo->next_restart_num;
  774.   int action = 1;
  775.  
  776.   /* Always put up a warning. */
  777.   WARNMS2(cinfo->emethods,
  778.       "Corrupt JPEG data: found 0x%02x marker instead of RST%d",
  779.       marker, desired);
  780.   /* Outer loop handles repeated decision after scanning forward. */
  781.   for (;;) {
  782.     if (marker < M_SOF0)
  783.       action = 2;        /* invalid marker */
  784.     else if (marker < M_RST0 || marker > M_RST7)
  785.       action = 3;        /* valid non-restart marker */
  786.     else {
  787.       if (marker == (M_RST0 + ((desired+1) & 7)) ||
  788.       marker == (M_RST0 + ((desired+2) & 7)))
  789.     action = 3;        /* one of the next two expected restarts */
  790.       else if (marker == (M_RST0 + ((desired-1) & 7)) ||
  791.            marker == (M_RST0 + ((desired-2) & 7)))
  792.     action = 2;        /* a prior restart, so advance */
  793.       else
  794.     action = 1;        /* desired restart or too far away */
  795.     }
  796.     TRACEMS2(cinfo->emethods, 4,
  797.          "At marker 0x%02x, recovery action %d", marker, action);
  798.     switch (action) {
  799.     case 1:
  800.       /* Let entropy decoder resume processing. */
  801.       return;
  802.     case 2:
  803.       /* Scan to the next marker, and repeat the decision loop. */
  804.       marker = next_marker(cinfo);
  805.       break;
  806.     case 3:
  807.       /* Put back this marker & return. */
  808.       /* Entropy decoder will be forced to process an empty segment. */
  809.       JUNGETC(marker, cinfo);
  810.       JUNGETC(0xFF, cinfo);
  811.       return;
  812.     }
  813.   }
  814. }
  815.  
  816.  
  817. /*
  818.  * Finish up after a compressed scan (series of read_jpeg_data calls);
  819.  * prepare for another read_scan_header call.
  820.  */
  821.  
  822. METHODDEF void
  823. read_scan_trailer (decompress_info_ptr cinfo)
  824. {
  825.   /* no work needed */
  826. }
  827.  
  828.  
  829. /*
  830.  * Finish up at the end of the file.
  831.  */
  832.  
  833. METHODDEF void
  834. read_file_trailer (decompress_info_ptr cinfo)
  835. {
  836.   /* no work needed */
  837. }
  838.  
  839.  
  840. /*
  841.  * The method selection routine for standard JPEG header reading.
  842.  * Note that this must be called by the user interface before calling
  843.  * jpeg_decompress.  When a non-JFIF file is to be decompressed (TIFF,
  844.  * perhaps), the user interface must discover the file type and call
  845.  * the appropriate method selection routine.
  846.  */
  847.  
  848. GLOBAL void
  849. jselrjfif (decompress_info_ptr cinfo)
  850. {
  851.   cinfo->methods->read_file_header = read_file_header;
  852.   cinfo->methods->read_scan_header = read_scan_header;
  853.   /* For JFIF/raw-JPEG format, the user interface supplies read_jpeg_data. */
  854. #if 0
  855.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  856. #endif
  857.   cinfo->methods->resync_to_restart = resync_to_restart;
  858.   cinfo->methods->read_scan_trailer = read_scan_trailer;
  859.   cinfo->methods->read_file_trailer = read_file_trailer;
  860. }
  861.  
  862. #endif /* JFIF_SUPPORTED */
  863.